package com.jzr.pda.db1.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.pda.common.SecurityHelper;
import com.jzr.pda.db1.mapper.TDoctorOrderMapper;
import com.jzr.pda.db1.model.TDoctorOrder;
import com.jzr.pda.db1.service.TDoctorOrderService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TDoctorOrderServiceImpl extends ServiceImpl<TDoctorOrderMapper,TDoctorOrder> implements TDoctorOrderService {
    public List<Integer> getIdList(Wrapper wrapper) {
        return baseMapper.getIdList(wrapper);
    }
    @Override
    public Integer getId(Wrapper wrapper) {
        List<Integer> list = getIdList(wrapper);
        return list == null || list.size() <= 0 ? null : list.get(0);
    }
    @Override
    public int exec(String orderNo) {
        Integer userId = SecurityHelper.getUserId();
        TDoctorOrder record = new TDoctorOrder();
        record.setIsExec(true);
        record.setExecTime(LocalDateTime.now());
        record.setExecUserId(userId);
        record.setUpdateTime(LocalDateTime.now());
        record.setUpdateBy(userId);

        UpdateWrapper<TDoctorOrder> uw = new UpdateWrapper<>();
        uw.eq(TDoctorOrder.OrderNo_,orderNo)
                .eq(TDoctorOrder.IsExec_,false);

        return baseMapper.update(record,uw);
    }

    @Override
    public Integer cancelExec(String orderNo) {
        Integer userId = SecurityHelper.getUserId();

        TDoctorOrder record = new TDoctorOrder();
        record.setIsExec(false);
        record.setUpdateTime(LocalDateTime.now());
        record.setUpdateBy(userId);

        UpdateWrapper<TDoctorOrder> uw = new UpdateWrapper<>();
        uw.set("exec_time",null);
        uw.set("exec_user_id",null);
        uw.eq(TDoctorOrder.OrderNo_,orderNo)
                .eq(TDoctorOrder.IsExec_,true);

        return baseMapper.update(record,uw);
    }

}
